Open
Conversation
CheezItMan
reviewed
Sep 2, 2020
CheezItMan
left a comment
There was a problem hiding this comment.
Overall well done, but you have a bug in your add method. Take a look at my comments and let me know what questions you have.
Comment on lines
+33
to
+43
| if @root == nil | ||
| @root = TreeNode.new(key, value) | ||
| return | ||
| end | ||
|
|
||
| node = add_helper(@root, key) | ||
| if key <= node.key | ||
| node.left = TreeNode.new(key, value) | ||
| else | ||
| node.right = TreeNode.new(key, value) | ||
| end |
There was a problem hiding this comment.
This doesn't actually work, you only can construct a 3-node tree your way.
Consider:
Suggested change
| if @root == nil | |
| @root = TreeNode.new(key, value) | |
| return | |
| end | |
| node = add_helper(@root, key) | |
| if key <= node.key | |
| node.left = TreeNode.new(key, value) | |
| else | |
| node.right = TreeNode.new(key, value) | |
| end | |
| @root = add_helper(@root, key, value) | |
| end | |
| def add_helper(current, key, value) | |
| return TreeNode.new(key, value) if current.nil? | |
| if key <= current.key | |
| current.left = add_helper(current.left, key, value) | |
| else | |
| current.right = add_helper(current.right, key, value) | |
| end | |
| return current | |
| end |
| return current.value if current.key == key | ||
| find_helper(current.left, key) | ||
| elsif key > current.key | ||
| return current.value if current.key == key |
There was a problem hiding this comment.
This can't be true.
Suggested change
| return current.value if current.key == key |
Comment on lines
75
to
78
| # Time Complexity: | ||
| # Space Complexity: | ||
| # left, root, right | ||
| def inorder |
Comment on lines
91
to
93
| # Time Complexity: | ||
| # Space Complexity: | ||
| def preorder |
Comment on lines
106
to
108
| # Time Complexity: | ||
| # Space Complexity: | ||
| def postorder |
Comment on lines
120
to
122
| # Time Complexity: | ||
| # Space Complexity: | ||
| def height |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
did not implement height successfully